package com.xsb.mvvmxsb.viewmodel

import android.annotation.SuppressLint
import android.view.View
import androidx.databinding.ObservableField
import com.xsb.mvvmlibrary.base.BaseViewModel
import com.xsb.mvvmlibrary.utils.SingleLiveEvent
import com.xsb.mvvmlibrary.base.BaseObserver
import com.xsb.mvvmxsb.api.Apiserver
import com.xsb.mvvmxsb.bean.BannerResponse
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 *     author : 薛少博
 *     e-mail : xsb@zlhopesun.com
 *     time   : 2021/5/19
 *     desc   : 类说明
 */
class MainViewModel : BaseViewModel() {
    //首页轮播图数据
    val bannerData: SingleLiveEvent<BannerResponse> = SingleLiveEvent()
    var title = ObservableField<String>()
    var geData = View.OnClickListener() {
        getBannerData()
    }

    /**
     * 获取轮播图数据
     * */
    fun getBannerData() {
        create(Apiserver::class.java).getBanner().subscribeOn(Schedulers.io())
            .doOnSubscribe({ showDialog.postValue("加载中...") })
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally({ hideDialog.postValue(true) })
            .subscribeWith(object : BaseObserver<BannerResponse>(){
                override fun onSuccess(data: BannerResponse) {
                    bannerData.postValue(data)
                   title.set(data.data.get(0).title)
                }

                override fun onError(msg: String?, code: Int) {
                    
                }
            })
    }
}